import json

from django.contrib.auth.decorators import login_required
from django.core import serializers
from django.http import JsonResponse
from django.shortcuts import render

from accounts.util import params_of_request
from school.serializers import SchoolSeri
from student.models import Student
from student.serializers import StudentSerializer
from teacher.models import Teacher
from teacher.serializers import TeacherSeri
from .models import School


# Create your views here.
def user_schools(user):
    schools = School.objects.filter(owner=user)

    teacher = Teacher.objects.filter(me=user)
    if teacher:
        schools |= teacher[0].teacher_schools.all()
        schools = schools.distinct()

    student = Student.objects.filter(me=user)
    if student:
        try:
            schools |= student[0].student_schools.all()
            schools = schools.distinct()
        except Exception as e:
            pass

    return schools


@login_required
def list(request):
    schools = user_schools(request.user)
    return render(request, 'school.html', {'user': request.user, 'schools': schools})


def detail(request):
    school_id = request.GET.get('id', 0)
    school = {}
    try:
        school = School.objects.get(pk=school_id)
    except Exception as e:
        pass
    return render(request, 'school-detail.html', {'user': request.user, 'school': school})


@login_required
def list_data(request):
    response = {}
    try:
        user = request.user
        schools = School.objects.filter(owner=user)

        teacher = Teacher.objects.filter(me=user)
        if teacher:
            schools |= teacher[0].teacher_schools.all()
            schools = schools.distinct()

        student = Student.objects.filter(me=user)
        if student:
            schools |= student[0].student_schools.all()
            schools = schools.distinct()

        response['list'] = json.loads(serializers.serialize("json", schools))
        response['msg'] = 'success'
    except Exception as e:
        response['msg'] = str(e)
    return JsonResponse(response)


@login_required
def detail_data(request):
    response = {}

    school_id = request.GET.get('id', 0)
    school = School.objects.filter(pk=school_id)
    response['school'] = json.loads(serializers.serialize("json", school))

    return JsonResponse(response)


def teachers(request):
    response = {}

    try:
        school_id = params_of_request(request).get('id', 0)
        school = School.objects.get(pk=school_id)
        teachers = school.teachers.all()
        response['teachers'] = TeacherSeri(teachers, many=True, context={'excludes': ['students', 'teacher_schools']}).data
    except Exception as e:
        response['msg'] = str(e)

    return JsonResponse(response)


def students(request):
    response = {}

    try:
        school_id = params_of_request(request).get('id', 0)
        school = School.objects.get(pk=school_id)
        students = school.students.all()

        response['students'] = StudentSerializer(students, many=True).data
    except Exception as e:
        response['msg'] = str(e)

    return JsonResponse(response)


def c_detail(request):
    school_jsons = json.loads(request.body).get('schools', None)
    schools = []
    for school_json in school_jsons:
        school_id = school_json.get('pk', None)
        if school_id:
            try:
                school = School.objects.get(pk=int(school_id))
                schools.append(school)
            except Exception as e:
                pass
    return JsonResponse({'schools': SchoolSeri(schools, many=True).data}, safe=False)
